package com.grpc;

import hello.HelloRequest;
import hello.HelloResponse;
import hello.HelloResponseList;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class HelloServer {

    private Server server;

    private void start() throws IOException {
        /* The port on which the server should run */
        int port = 50051;
        server = ServerBuilder.forPort(port)
                .addService(new HelloIml())  //这里可以添加多个模块
                .build()
                .start();
        System.out.println("Server started, listening on " + port);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                // Use stderr here since the logger may have been reset by its JVM shutdown hook.
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                try {
                    HelloServer.this.stop();
                } catch (InterruptedException e) {
                    e.printStackTrace(System.err);
                }
                System.err.println("*** server shut down");
            }
        });
    }

    private void stop() throws InterruptedException {
        if (server != null) {
            server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
        }
    }

    private void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        final HelloServer server = new HelloServer();
        server.start();
        server.blockUntilShutdown();
    }

    private static class HelloIml extends HelloGrpc.HelloImplBase {
        //一元流
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            responseObserver.onNext(HelloResponse.newBuilder().setMessage("服务端:小明好").build());
            responseObserver.onCompleted();
        }

        //服务流
        @Override
        public void serviceStreamHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            responseObserver.onNext(HelloResponse.newBuilder().setMessage("服务端：小明好").build());
            responseObserver.onNext(HelloResponse.newBuilder().setMessage("服务端：你吃饭了吗").build());
            responseObserver.onNext(HelloResponse.newBuilder().setMessage("服务端：最近怎么样").build());
            responseObserver.onCompleted();
        }

        //客户流
        @Override
        public StreamObserver<HelloRequest> clientStreamHello(StreamObserver<HelloResponseList> responseObserver) {

            return new StreamObserver<HelloRequest>() {

                @Override
                public void onNext(HelloRequest helloRequest) {//获取的
                    System.out.println(helloRequest.getName());
                }

                @Override
                public void onError(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }

                @Override
                public void onCompleted() {
                    HelloResponse helloResponse1 = HelloResponse.newBuilder().setMessage("服务端：你好小华").build();
                    HelloResponse helloResponse2 = HelloResponse.newBuilder().setMessage("服务端:已经吃了").build();
                    HelloResponseList list = HelloResponseList.newBuilder()
                            .addHelloResponse(helloResponse1)
                            .addHelloResponse(helloResponse2)
                            .build();
                    responseObserver.onNext(list);
                    responseObserver.onCompleted();
                }
            };
        }

        //客户服务流
        @Override
        public StreamObserver<HelloRequest> clientServiceStreamHello(StreamObserver<HelloResponse> responseObserver) {
            return new StreamObserver<HelloRequest>() {
                @Override
                public void onNext(HelloRequest helloRequest) {
                    System.out.println("服务端接受：" + helloRequest.getName());
                    responseObserver.onNext(HelloResponse.newBuilder().setMessage("接受到了" + helloRequest.getName()).build());
                }

                @Override
                public void onError(Throwable t) {
                    System.out.println(t.getMessage());
                }

                @Override
                public void onCompleted() {

                    responseObserver.onCompleted();
                }
            };
        }


    }
}